package com.kh.pajx.sop.view.listener;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.kh.pajx.sop.domain.SysArea;
import com.kh.pajx.sop.domain.SysDictionary;
import com.kh.pajx.sop.domain.SysMenuAuth;
import com.kh.pajx.sop.service.ISysMenuAuthService;
import com.kh.pajx.sop.service.sys.ISysAreaService;
import com.kh.pajx.sop.service.sys.ISysDictionaryService;

/**
 * 获取权限菜单信息的监听器
 * @author:	azzcsimp
 * @Createdate:	2014年7月30日 下午4:09:35
 */
public class InitServletContextListener implements ServletContextListener {

	@Override
	public void contextInitialized(ServletContextEvent sce) {
		ServletContext application = sce.getServletContext();

		// 得到Service的实例对象
		ApplicationContext ac = WebApplicationContextUtils
				.getWebApplicationContext(application);
		ISysMenuAuthService sysMenuAuthService = (ISysMenuAuthService) ac
				.getBean("sysMenuAuthServiceImpl");
		
		// 初始化区域信息
		ISysAreaService areaService = (ISysAreaService) ac.getBean("sysAreaServiceImpl");
		try {
			List areaList=areaService.findAll();
			application.setAttribute("areaList", areaList);
			Map<String,String> areaMap = areaService.getAreaMap();
			application.setAttribute("areaMap",areaMap);
			Map<String,String> areaParentMap = areaService.getParentAreaMap();
			application.setAttribute("areaParentMap",areaParentMap);
			Map<String, List<SysArea>> areaChild=areaService.getAreaMap2();
			application.setAttribute("areaChild",areaChild);
			System.out.println("平安家校系统运维平台(SOP)：=======地区信息初始化完毕！("+areaMap.size()+")=========");
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// 准备所有顶级权限集合(顶级菜单)
		List<SysMenuAuth> topMenuAuthList = sysMenuAuthService
				.findTopMenuListBySys("SOP");
		application.setAttribute("topMenuAuthList", topMenuAuthList);
		System.out.println("平安家校系统运维平台(SOP)：=======顶级菜单初始化完毕！("+topMenuAuthList.size()+")=========");
		
		// 准备所有二级主菜单
		List<SysMenuAuth> secondMenuAuthList = sysMenuAuthService.findSecondMenuAuthListBySystemCode("SOP");
		application.setAttribute("secondMenuAuthList", secondMenuAuthList);
		System.out.println("平安家校系统运维平台(SOP)：=======二级菜单初始化完毕！("+secondMenuAuthList.size()+")=========");

		// 准备所有权限URL集合
		//List<String> allMenuAuthUrls = sysMenuAuthService.getAllMenuAuthUrls();
		List<String> allMenuAuthUrls = sysMenuAuthService.getAllMenuAuthUrlsBySystemCode("SOP");
		application.setAttribute("allMenuAuthUrls", allMenuAuthUrls);
		System.out.println("平安家校系统运维平台(SOP)：=======权限的URL数据初始化完毕！("+allMenuAuthUrls.size()+")=========");
		
		// 初始化补卡单据流程状态信息
		Map<Integer,String> orderProgressStatus = new HashMap<Integer, String>();
		orderProgressStatus.put(1, "单据草稿");
		orderProgressStatus.put(2, "待审核");
		orderProgressStatus.put(3, "审核中");
		orderProgressStatus.put(4, "审核通过");
		orderProgressStatus.put(5, "审核驳回");
		orderProgressStatus.put(6, "正在制卡");
		orderProgressStatus.put(7, "已发卡");
		orderProgressStatus.put(8, "完成");
		application.setAttribute("orderProgressStatus", orderProgressStatus);
		System.out.println("平安家校系统运维平台(SOP)：=======补卡单据流程状态信息初始化完毕！("+orderProgressStatus.size()+")=========");
		//初始化字典信息
		ISysDictionaryService sysDic = (ISysDictionaryService) ac.getBean("sysDictionaryServiceImpl");
		Map<String,List<SysDictionary>> mapDic = sysDic.getEveryDicType();
		application.setAttribute("dictionary", mapDic);
		System.out.println("平安家校系统运维平台(SOP)：=======字典列表加载完毕=======！");
		
		// 初始化结算类型
		Map<Integer,String> balTypeMap = new HashMap<Integer, String>();
		balTypeMap.put(1, "维护人员");
		balTypeMap.put(2, "扩展人员");
		balTypeMap.put(3, "学校话费");
		balTypeMap.put(4, "学校现金");
		balTypeMap.put(5, "客户经理");
		balTypeMap.put(6, "行业经理");
		application.setAttribute("balTypeMap", balTypeMap);
		System.out.println("平安家校系统运维平台(SOP)：=======结算类型初始化完毕=======！");
		
		Map<String,String> deptMap = new HashMap<String, String>();
		deptMap.put("1003", "市场一部");
		deptMap.put("1004", "市场二部");
		deptMap.put("1005", "市场三部");
		deptMap.put("1006", "市场四部");
		application.setAttribute("deptMap", deptMap);
		
		//初始化快递类型
		Map<String,String> expressMap = new HashMap<String,String>();
		expressMap.put("shunfeng", "顺丰快递");
		expressMap.put("yuantong", "圆通快递");
		expressMap.put("yunda", "韵达快递");
		expressMap.put("shentong", "申通快递");
		expressMap.put("huitongkuaidi", "百世汇通");
		expressMap.put("tiantian", "天天快递");
		expressMap.put("zhongtong", "中通快递");
		expressMap.put("ems", "EMS");
		expressMap.put("youzhengguonei", "邮政小包");
		expressMap.put("zhaijisong", "宅急送");
		application.setAttribute("expressMap", expressMap);
		System.out.println("平安家校系统运维平台(SOP)：=======快递类型初始化完毕=======！");
	}

	@Override
	public void contextDestroyed(ServletContextEvent sce) {

	}

}
